import openpyxl
import requests
from bs4 import BeautifulSoup

# 创建工作簿和工作表
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = 'Top250'

# 爬取数据
url = 'https://movie.douban.com/top250'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/91.0.4472.124 Safari/537.36 '
}

for i in range(10):
    params = {
        'start': i * 25
    }

    response = requests.get(url, headers=headers, params=params)
    soup = BeautifulSoup(response.text, 'html.parser')

    # 解析数据
    movies = soup.find_all('div', class_='info')

    # print(movies)

    for movie in movies:
        title = movie.find('span', class_='title').text
        rank = movie.find('a')["href"]
        info = movie.find('div', class_='bd').p.text.strip().split('\n')[0]
        sheet.append([title, rank, info])

# 保存数据到Excel文件
workbook.save('douban_movie_top250.xlsx')
workbook.close()
